<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Carbon\Carbon;

class CashierGoods extends Model
{
    protected $table = 'ims_yz_cashier_goods';
    
    protected $fillable = [
        'goods_id',
        'is_open',
        'shop_commission',
        'settlement_day',
        'is_write_information',
        'is_cash_pay',
        'plugins',
        'shop_award_point',
        'profit',
        'coupon',
        'payment_types',
        'shop_award_point1',
        'shop_award_point2',
        'audio_open',
        'pay_reward',
        'fixed_meney',
        'max_pay_number',
        'every_min_money',
        'face_pay',
        'is_open_member_discount',
        'pay_reward_balance',
        'created_at',
        'updated_at',
        'deleted_at'
    ];

    protected $casts = [
        'goods_id' => 'integer',
        'is_open' => 'boolean',
        'shop_commission' => 'decimal:5',
        'settlement_day' => 'integer',
        'is_write_information' => 'boolean',
        'is_cash_pay' => 'boolean',
        'shop_award_point' => 'decimal:2',
        'shop_award_point1' => 'decimal:2',
        'shop_award_point2' => 'decimal:2',
        'audio_open' => 'integer',
        'fixed_meney' => 'decimal:2',
        'max_pay_number' => 'integer',
        'every_min_money' => 'decimal:2',
        'face_pay' => 'integer',
        'is_open_member_discount' => 'integer',
        'created_at' => 'integer',
        'updated_at' => 'integer',
        'deleted_at' => 'integer'
    ];

    public $timestamps = true;

    public function getDateFormat()
    {
        return 'U';
    }

    public function getCreatedAtAttribute($value)
    {
        return $value ? Carbon::createFromTimestamp($value) : null;
    }

    public function getUpdatedAtAttribute($value)
    {
        return $value ? Carbon::createFromTimestamp($value) : null;
    }

    /**
     * 获取商品
     */
    public function goods(): BelongsTo
    {
        return $this->belongsTo(Goods::class, 'goods_id', 'id');
    }

    /**
     * 查询开启的收银台商品
     */
    public function scopeOpen($query)
    {
        return $query->where('is_open', 1);
    }

    /**
     * 查询需要填写信息的商品
     */
    public function scopeNeedInformation($query)
    {
        return $query->where('is_write_information', 1);
    }

    /**
     * 查询支持现金支付的商品
     */
    public function scopeSupportCashPay($query)
    {
        return $query->where('is_cash_pay', 1);
    }

    /**
     * 查询开启会员折扣的商品
     */
    public function scopeMemberDiscount($query)
    {
        return $query->where('is_open_member_discount', 0);
    }

    /**
     * 获取开启状态文本
     */
    public function getOpenStatusTextAttribute(): string
    {
        return $this->is_open ? '开启' : '关闭';
    }

    /**
     * 获取现金支付状态文本
     */
    public function getCashPayStatusTextAttribute(): string
    {
        return $this->is_cash_pay ? '支持' : '不支持';
    }

    /**
     * 获取会员折扣状态文本
     */
    public function getMemberDiscountStatusTextAttribute(): string
    {
        return $this->is_open_member_discount == 0 ? '开启' : '关闭';
    }
}
